package com.goods.business.service.imp;

import com.goods.business.mapper.BizConsumerMapper;
import com.goods.business.service.BizConsumerService;
import com.goods.common.model.business.BizConsumer;
import com.goods.common.model.business.BizSupplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BizConsumerServiceImpl implements BizConsumerService {

    @Autowired
    private BizConsumerMapper bizConsumerMapper;

    @Override
    public List<BizConsumer> findAllConsumer() {
        Example example = new Example(BizConsumer.class);
        List<BizConsumer> bizConsumerList = bizConsumerMapper.selectByExample(example);
        return bizConsumerList;
    }

    @Override
    public PageVO<ConsumerVO> findConsumerList(Map<String, Object> queryMap) {
        String name = (String) queryMap.get("name");
        String address = (String) queryMap.get("address");
        String contact = (String) queryMap.get("contact");
        Example example = new Example(BizConsumer.class);
        /*添加条件*/
        if (name != null && !name.equals(""))
            example.createCriteria().andEqualTo("name", name);
        if (address != null && !address.equals(""))
            example.createCriteria().andEqualTo("address", address);
        if (contact != null && !contact.equals(""))
            example.createCriteria().andEqualTo("contact", contact);
        List<BizConsumer> bizConsumerList = bizConsumerMapper.selectByExample(example);
        List<ConsumerVO> consumerVOS = new ArrayList<>();
        for (BizConsumer bizConsumer : bizConsumerList) {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(bizConsumer, consumerVO);
            consumerVOS.add(consumerVO);
        }
        List<ConsumerVO> page =null;
        if (queryMap.get("pageNum") !=null && queryMap.get("pageSize") != null){
            page = ListPageUtils.page(consumerVOS, Integer.parseInt((String) queryMap.get("pageSize")), Integer.parseInt((String) queryMap.get("pageNum")));
            return new PageVO<>(bizConsumerList.size(), page);
        }
        return new PageVO<>(bizConsumerList.size(),consumerVOS);
    }

    @Override
    public void addConsumer(ConsumerVO consumerVO) {
        BizConsumer bizConsumer = new BizConsumer();
        BeanUtils.copyProperties(consumerVO, bizConsumer);
        bizConsumerMapper.insert(bizConsumer);
    }

    @Override
    public ConsumerVO getConsumerVo(Integer id) {
        BizConsumer bizConsumer = bizConsumerMapper.selectByPrimaryKey(id);
        ConsumerVO consumerVO = new ConsumerVO();
        if (bizConsumer != null) {
            BeanUtils.copyProperties(bizConsumer, consumerVO);
        }
        return consumerVO;
    }

    @Override
    public void updateBizConsumer(Integer id, BizConsumer bizConsumer) {
        BizConsumer bizConsumer1 = bizConsumerMapper.selectByPrimaryKey(id);
        if (bizConsumer1 != null) {
            bizConsumer.setId(bizConsumer1.getId());
            bizConsumerMapper.updateByPrimaryKey(bizConsumer);
        }
    }

    @Override
    public void deleteConsumerById(Integer id) {
        bizConsumerMapper.deleteByPrimaryKey(id);
    }
}
